package utilBean;

import java.util.List;
import java.util.logging.Logger;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;

import models.Area;
import models.Usuario;

import org.primefaces.event.FlowEvent;
import org.primefaces.event.SelectEvent;

import dao.AreaDAO;
import dao.UsuarioDAO;

public class UserWizard {
	
	private boolean skip;  
    
    private static Logger logger = Logger.getLogger(UserWizard.class.getName());  
	
	private Usuario usuario = new Usuario();
	
	private List<Area> areas = AreaDAO.findAll();
	
	private int id_area;
	
	private String cargo;
	
	public String getCargo() {
		return cargo;
	}

	public void setCargo(String cargo) {
		this.cargo = cargo;
	}

	private Area area_selected;

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public void save(ActionEvent actionEvent) {
		usuario.setArea(area_selected);
		usuario.setCargo(cargo);
		UsuarioDAO.persistUser(usuario);
        FacesMessage msg = new FacesMessage("Successful", "Usuario: " + usuario.getCodigo()+ " registrado con exito.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    } 
    
    public boolean isSkip() {  
        return skip;  
    }  
  
    public void setSkip(boolean skip) {  
        this.skip = skip;  
    } 
    
	public String onFlowProcess(FlowEvent event) {  
        logger.info("Current wizard step:" + event.getOldStep());  
        logger.info("Next step:" + event.getNewStep());  
          
        if(skip) {  
            skip = false;   //reset in case user goes back  
            return "confirm";  
        }  
        else {  
            return event.getNewStep();  
        }  
    }

	public void onItemSelectArea(final AjaxBehaviorEvent event){
		area_selected = areas.get(id_area);
	}
	
	public void onItemSelectCargo(final AjaxBehaviorEvent event){
		
	}
	
	public void onRowUnselect(SelectEvent event){
	}

	public Area getArea_selected() {
		return area_selected;
	}

	public void setArea_selected(Area area_selected) {
		this.area_selected = area_selected;
	}

	public int getId_area() {
		return id_area;
	}

	public void setId_area(int id_area) {
		this.id_area = id_area;
	}

	public List<Area> getAreas() {
		return areas;
	}

	public void setAreas(List<Area> areas) {
		this.areas = areas;
	}

	
	
}
